#!/usr/bin/python
# -*- coding:utf-8 -*-
# @Time  : 7/10/20
# @Author: yanlh
# @usage : 扩展插件样例
import sys

sys.path.append('..')
from common_utils import CBBCommonUtils


def get_plugin_info():
    plugin_info = {
        "name": "extend_test_sample test",
        "plugin_id": "extend_test_sample",
        "plugin_type": "extend_test_sample",
        "info": "a sample for writing an extend plugin",
        "level": "B",
        "module": "extend test",
        "author": "yanlh",
        "keyword": "sample",
        "configable": "false",
    }
    return plugin_info


logger = None
cur_user = None
cur_module = None
cur_task = None


def set_plugin_logger(setter, user, module, task, *args, **kwargs):
    global logger, cur_user, cur_module, cur_task
    logger = setter
    cur_user = user
    cur_module = module
    cur_task = task


# 扫描函数
def scan(ip, system_user, system_pwd, flag=0):
    logger.debug_info(cur_user, cur_module, cur_task + 'info', '', "Scan Start.")
    scan_list = []
    error_count = 0
    '''
    该插件扫描的功能实现
    '''
    logger.debug_info(cur_user, cur_module, cur_task + 'info', '', 'Scan Complete.')
    if flag:
        reinforce_list, reinforce_error = reinforce(ip, system_user, system_pwd)
        scan_list.extend(reinforce_list)
        return scan_list, reinforce_error
    return scan_list, error_count


def reinforce(ip, system_user, system_pwd):
    logger.debug_info(cur_user, cur_module, cur_task + 'info', '', 'Reinforce Start.')
    reinforce_list = []
    error_count = 0
    '''
    该插件加固的功能实现
    '''
    logger.debug_info(cur_user, cur_module, cur_task + 'info', '', 'Reinforce Complete.')
    return reinforce_list, error_count


# 回滚函数
def rollback(ip, system_user, system_pwd):
    logger.debug_info(cur_user, cur_module, cur_task + 'info', '', 'rollback Start.')
    rollback_list = []
    error_count = 0
    '''
    该插件回退的功能实现
    '''
    logger.debug_info(cur_user, cur_module, cur_task + 'info', '', 'rollback Complete.')
    return rollback_list, error_count


def check(ip, *args, **kwargs):
    sys_user = kwargs.get("system_user")
    sys_pwd = kwargs.get("system_pwd")
    comm = kwargs.get("command")
    try:
        des_list = []
        logger.debug_info(cur_user, cur_module, cur_task + '_Check', '', 'safe_extend_test_sample start')
        if comm == 1:
            des_scan, error_scan = scan(ip, sys_user, sys_pwd, flag=0)
            des_list.extend(des_scan)
            step_error = int(error_scan)
        elif comm == 2:
            des_reinforce, error_reinforce = scan(ip, sys_user, sys_pwd, flag=1)
            des_list.extend(des_reinforce)
            step_error = int(error_reinforce)
        elif comm == 3:
            des_rollback, error_rollback = rollback(ip, sys_user, sys_pwd)
            des_list.extend(des_rollback)
            step_error = int(error_rollback)
        else:
            return {"code": 3, "count": 0, "des": ['command must be 1/2/3']}
        logger.debug_info(cur_user, cur_module, cur_task + '_Check', '', 'safe_extend_test_sample Complete')
        if step_error == 0:
            code = 0
        elif step_error <= -1:
            code = 2
        else:
            code = 1
        return {"code": code, "count": step_error, "des": des_list}
    except Exception as er:
        code = 1
        des = ["ERROR:", str(er)]
        logger.debug_error(cur_user, cur_module, cur_task + '_Check', '', des)
        return {"code": code, "count": 0, "des": des}
